package com.dating.view;

import java.net.MalformedURLException;
import java.net.URL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dating.activity.R;
import com.dating.entities.Her;
import com.dating.entities.RowChat;
import com.dating.libs.client.parttern.doit.OnTaskListener;
import com.dating.pref.Function;

@SuppressLint({ "ViewConstructor", "ViewConstructor" })
public class RowChatLayout extends LinearLayout implements OnTaskListener{
	ImageView avatar;
	TextView content,time;
	LinearLayout chat;
	
	RowChat chatData=null;
	Her her;
	
	LayoutInflater inflater=null;
	OnTaskListener _listener;
	Handler _handler;
	public RowChatLayout(Context context,Her her,RowChat data) {
		super(context);
		
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(data.tag.equals("her"))
			inflater.inflate(R.layout.item_chat, this);
		else
			inflater.inflate(R.layout.item_chat_me, this);
		chatData=data;
		this.her=her;
		
		findById();
		
	}
	private void findById(){
		avatar=(ImageView)findViewById(R.id.avatar);
		content=(TextView)findViewById(R.id.content);
		time=(TextView)findViewById(R.id.time);
		chat=(LinearLayout)findViewById(R.id.layout_chat);
	}
	public void setOnTaskListener(OnTaskListener listener){
		this._listener=listener;
	}
	public void execute(Handler h){
		this._handler=h;
		synchronized (this._listener) {
			this._listener.onPrepare("");
		}
	}
	@Override
	public void onPrepare(String... params) {
//		synchronized (this._handler) {
//			new Thread(new Runnable() {
//				
//				@Override
//				public void run() {
//					onDoBackground("avatar");
//				}
//			}).start();
//			new Thread(new Runnable() {
//				
//				@Override
//				public void run() {
//					onDoBackground("content");
//				}
//			}).start();
//		}
		onDoBackground("content");
	}
	@Override
	public void onDoBackground(String... params) {
		if(params[0].equals("avatar")){
			// avatar (new thread)
			try {
				URL url = new URL(chatData.avatar);
				if(url.toString().contains("http")){
					chatData.bmp=Function.getBitmapFromUrl(url);
				}
				onComplete(true);
				return;
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		else if(params[0].equals("content")){
			// content
			if(chatData.content.equals(""))
				return;
			// time
			onComplete(true);
		}
	}
	@Override
	public void onComplete(Boolean values) {
		if(values)
			if(this._handler!=null){
				this._handler.post(new Runnable() {
					
					@Override
					public void run() {
						if(chatData.bmp==null) avatar.setImageResource(R.drawable.noavatar);
						else avatar.setImageBitmap(chatData.bmp);
						
						content.setText(chatData.content);
						time.setText(chatData.time);
						
						//time
						time.setVisibility(View.INVISIBLE);
					}
				});
			}
	}
}
